package com.fzdy.dataplatform.dao.impl;

import com.fzdy.dataplatform.core.dao.impl.BaseDaoImpl;
import com.fzdy.dataplatform.dao.KTVDao;
import com.fzdy.dataplatform.entity.KTV;
import org.hibernate.Criteria;
import org.hibernate.criterion.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class KTVDaoImpl extends BaseDaoImpl<KTV> implements KTVDao {

    @Override
    public List<KTV> findByExampleLike(KTV example, int pageNo, int pageSize) {
        Criteria criteria = this.getSession().createCriteria(this.clazz).add(Example.create(example)
                .enableLike(MatchMode.ANYWHERE));
        criteria.add(Restrictions.or(Restrictions.eq("share", 1),
                Restrictions.eq("userid", example.getUserid())));
        return this.createPageOrder(criteria, pageNo, pageSize, Order.desc("id")).list();
    }

    @Override
    public long countByExampleLike(KTV example) {
        Criteria criteria = this.getSession().createCriteria(this.clazz).add(Example.create(example)
                .enableLike(MatchMode.ANYWHERE));
        criteria.add(Restrictions.or(Restrictions.eq("share", 1),
                Restrictions.eq("userid",example.getUserid())));
        Long o = (Long) criteria.setProjection(Projections.count("id")).uniqueResult();
        return o.longValue();
    }

    @Override
    public List<KTV> getList(Integer id) {
        Criteria crit = this.getSession().createCriteria(this.clazz);
        crit.add(Restrictions.or(Restrictions.eq("share", 1),
                Restrictions.eq("userid",id)));
        List<KTV> list=crit.list();
        return list;
    }
}
